package com.yunhe.servlet;

import com.yunhe.enity.Course;
import com.yunhe.service.serviceimpl.CourseServiceImpl;
import com.yunhe.utils.*;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/courses")
@MultipartConfig
public class CourseServlet extends BaseServlet {
    CourseServiceImpl service = new CourseServiceImpl();
    Course course = new Course();
    ResultVu vu = null;

    public void uploadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用工具上传文件
        String fileName = UploadUtils.uploadFile(req);
        //判断文件后缀是图片还是视频
        if (fileName.endsWith("jpg") || fileName.endsWith("jpeg")) {
            vu = new ResultVu(200, "上传图片成功", fileName);
        } else {
            vu = new ResultVu(200, "上传视频成功", fileName);
        }
        JsonUtil.objToJson(vu, resp);
    }

    public void removeFile(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取要删除的文件资源
        String fileName = req.getParameter("fileName");
        //调用工具类截取文件名
        fileName = FileNameUtils.subFileName(fileName);
        //根据文件路径和文件名称构建file文件对象
        File file = new File("E:/file/" + fileName);
        //删除文件
        file.delete();
        //判断文件后缀是图片还是视频
        if (fileName.endsWith("jpg")) {
            vu = new ResultVu(200, "删除图片成功", null);
        } else {
            vu = new ResultVu(200, "删除视频成功", null);
        }
        //转json并且响应
        JsonUtil.objToJson(vu, resp);
    }

    public void addCourse(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        //获取参数，接收参数到map中
        Map<String, String[]> map = req.getParameterMap();
        //创建实例对象
        Course course = new Course();
        //实用工具类封装对象
        BeanUtils.populate(course, map);
        //处理文件名
        course.setCourseImage(FileNameUtils.subFileName(course.getCourseImage()));
        course.setCourseVideo(FileNameUtils.subFileName(course.getCourseVideo()));
        //调用业务
        int row = service.addCourse(course);
        if (row > 0) {
            vu = new ResultVu(200, "课程添加成功", null);
        } else {
            vu = new ResultVu(500, "课程添加失败", null);
        }
        JsonUtil.objToJson(vu, resp);
    }

    public void findPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //用户想看的页码
        String currentPage = req.getParameter("currentPage");
        //每页显示条数
        String pageSize = req.getParameter("pageSize");
        //搜索条件
        String courseName = req.getParameter("courseName");
        //调用业务
        PageBeen<Course> pb = service.findPage(currentPage, pageSize, courseName);
        //响应数据
        JsonUtil.objToJson(pb, resp);
    }

    public void delAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String cid = req.getParameter("cid");
        int row = service.delAll(cid);
        System.out.println(row);
        if (row > 0) {
            vu = new ResultVu(200, "删除数据成功", null);
        } else {
            vu = new ResultVu(500, "删除数据失败", null);
        }
        JsonUtil.objToJson(vu, resp);
    }

    public void updateCourse(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        BeanUtils.populate(course, map);
        Course course1 = service.findCourse(course);
        if (!course1.getCourseImage().equals(course.getCourseImage())){
            File file = new File("E://file"+course1.getCourseImage());
            file.delete();
        }
        if (!course1.getCourseVideo().equals(course.getCourseVideo())){
            File file = new File("E://file"+course1.getCourseVideo());
            file.delete();
        }
        course.setCourseImage(FileNameUtils.subFileName(course.getCourseImage()));
        course.setCourseVideo(FileNameUtils.subFileName(course.getCourseVideo()));
        int row = service.updateCourse(course);
        if (row>0){
            vu = new ResultVu(200,"修改成功",null);
        }else {
            vu = new ResultVu(500,"修改失败",null);
        }
        JsonUtil.objToJson(vu,resp);
    }
}
